using System;

namespace DGen.Text
{
    /// <summary>
    /// Utility class for time related functions. This is utterly unnecessarily and I'm quietly seething at the fact
    /// I'm doing this.
    /// </summary>

    public static class Time
    {


        /// <summary>
        /// Milliseconds since 01/01/1970.
        /// Overloaded version for convenience / stupidity.
        /// </summary>
        /// <returns>
        /// A <see cref="System.Int64"/>
        /// </returns>
        public static long UnixEpochTime ()
        {
            return UnixEpochTime (DateTime.UtcNow );
        }
        /// <summary>
        /// Milliseconds since 01/01/1970.
        /// </summary>
        /// <param name="dt">
        /// A <see cref="DateTime"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.Int64"/>
        /// </returns>
        public static long UnixEpochTime (DateTime dt)
        {
            return (long)(dt.ToUniversalTime () - new DateTime (1970, 1, 1)).TotalMilliseconds;
        }
        /// <summary>
        /// Returns Unix Epoch Time as Human Readable string.
        /// Human readable is a fancy way of saying with a thousand seperator included.
        /// Humans can probably read 13 digit numbers unaided if they really need to.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public static string UnixEpochTimeString ()
        {
            return UnixEpochTimeString (DateTime.UtcNow);
        }
        /// <summary>
        /// Returns Unix Epoch Time as Human Readable string.
        /// Human readable is a fancy way of saying with a thousand seperator included.
        /// Humans can probably read 13 digit numbers unaided if they really need to.
        /// </summary>
        public static  string UnixEpochTimeString (DateTime dt)
        {
            return String.Format ("{0:0,0}", UnixEpochTime (dt));
        }

      private static long ToUnixEpoch(string dateTime)
        {
            DateTime dt = new DateTime();
            if (DateTime.TryParse(dateTime, out dt))
            {
                return ToUnixEpoch(dt);
            }
            else
            {
                return -1;
            }

        }
        private static long ToUnixEpoch(DateTime dt) { return (dt.ToUniversalTime().Ticks - 621355968000000000) / 10000000; }

    }
}

